﻿
using HBase;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace hCommand {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：TransferChain                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2020/10/10 9:12:53                        
   *└────────────────────────────────────────────────┘
   **/
    public class Estimate : RAM, CacheData {
        public DateTime beginDate;//占用时间
        private long beginTick = 0;
        public DateTime endDate;//释放时间
        private long endTick = 0; 
        public long taskId;
        public int taskNo; 
        private long conmonBeforeTime;
        private long conmonNextTime;
        private long currentTime;
        [JsonIgnore]
        private Node node;
        [JsonIgnore]
        private Estimate before;
        [JsonIgnore]
        private Estimate next; 
        [JsonIgnore]
        public TaskDto taskDto;

        public int getStopTime() { 
            long t = (endDate.Ticks - beginDate.Ticks) / 10000 - conmonBeforeTime - conmonNextTime - currentTime;
            return t > 0 ? (int)t : 0;

        }
        public Edge getNextReverseEdge() {
            List<Edge> edges = node.getNextEdge();
            foreach (Edge edge in edges) {
                if (edge.desNode == next.getNode()) {
                    return edge.reverseEdge;
                }
            }
            return null;
        }
        public long getConmonNextTime() {
            return conmonNextTime;
        }
        public void setConmonNextTime(long t) {
            conmonNextTime = t;
        }
       
        public void setBegin(DateTime dt) { 
            beginTick = dt.Ticks;
            beginDate = dt;

            endDate = beginDate.AddMilliseconds(conmonBeforeTime + currentTime + conmonNextTime);
            endTick = endDate.Ticks;
        } 
        public void setEndDate(DateTime dt) { 
            endDate = dt.AddMilliseconds(conmonNextTime); 
            endTick = endDate.Ticks;
        }
        public bool isIn(DateTime dt) {
            return beginTick <= dt.Ticks && dt.Ticks < endTick;
        } 
        public DateTime getMoveNext() {//本货物准备占用下一级时间，
            return beginDate.AddMilliseconds(conmonBeforeTime + currentTime);
        }
         

        public void removeBefore() {
            //node.clearRightOfWay(taskNo);
            Estimate b = before;
            while (b != null) {
                if (b.next != null) {
                    b.next.before = null;
                } 
                var bn = b.getNode();
                if (bn != null) {
                    bn.clearCurrent(b.id);
                }
              //  bn.clearRightOfWay(taskNo);
                b.next = null;
                b = b.before;

            }
            this.before = null;
        } 
        public Estimate(BaseTask t, Node node, long beforCommon, long currentTime, TaskDto taskDto) {
            this.currentTime = currentTime;
            conmonBeforeTime = beforCommon;
            taskId = t.getId();
            this.node = node;
            taskNo = t.taskNo;
            createId();
            node.addEstimate(this);
            this.taskDto = taskDto;
        }
        public void addBefore(Estimate before) {
            if (before != null) {
                before.next = this;
                this.before = before;
            }
        }



        public Node getNode() {
            return node;
        }

        public Estimate getNextEstimate() {
            return next;
        }

        public Estimate getBeforeEstimate() {
            return before;
        }

        public bool isFirst() {
            return before == null;
        }

    }
}
